// pages/cate/index.js
import { tips, confirm, coludRequest, formatDate } from '../util/util.js' 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCate();    
  },

  onUnload:function(){    
  },

  add:function(){
     wx.navigateTo({
       url: '/pages/article/add/index',
     })
  },
  
  //查询文章
  getCate:function(){
    var that = this;    
    coludRequest('admin', {module:'article', action:'query', map:{}},function(res){
      var arr = res.result.data;
      arr.forEach(item=>{
        item.datetime = formatDate('1551334252272');
      })
      that.setData({ articleList: arr });
    })
  },

  //编辑文章
  edit:function(e){
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/article/edit/index?id='+id,
    })
  },

  //删除文章
  delete:function(e){
     var id = e.target.dataset.id;
     var index = e.target.dataset.index;
     var that = this; 
     confirm('确认删除该文章吗？',function(res){
      if (res.confirm){
        coludRequest('admin', {module:'article', action:'delete', map: { _id: id } }, function (data) {
          tips('删除成功', 1200, 'success');
          setTimeout(res => {
            var list = that.data.articleList;
            list.splice(index, 1)
            that.setData({ articleList: list });
          }, 1200)
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})